import { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import { getMirrorOptions, type MirrorNames } from "@/modules/dev-env/mirror/data.ts";
import type { MirrorWebsiteFile } from "@common/spider-types.ts";
import type { ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import path from "@/framework/util/path.ts";
import semver from "semver";

export class RocketmqMirror extends BaseEnvMirror {
  constructor(name: MirrorNames) {
    super(getMirrorOptions(name, name === "apache" ? "/rocketmq/" : "/apache/rocketmq/"));
  }

  protected getDownloadList0(files: MirrorWebsiteFile[]): ToolInstallVersionItem[] | Promise<ToolInstallVersionItem[]> {
    const versionList: ToolInstallVersionItem[] = [];
    for (const it of files) {
      // 忽略2.0.0及以下版本
      if (it.type === "dir" && /\d+\.\d+\.\d+/.test(it.name) && +it.name[0] > 2) {
        // https://mirrors.aliyun.com/apache/rocketmq/5.3.3/rocketmq-all-5.3.3-bin-release.zip
        const downloadUrl = `${this.url}${it.name}/rocketmq-all-${it.name}-bin-release.zip`;
        versionList.push({
          id: `rocketmq-${it.name}`,
          name: path.basename(downloadUrl),
          version: it.name,
          url: downloadUrl,
          date: it.date,
          size: it.size,
        });
      }
    }

    versionList.sort((a, b) => semver.compare(b.version, a.version));
    return versionList;
  }
}
